﻿using System;
using System.Web;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;

namespace HeroldIT.Dnn.Modules
{
    /// <summary>
    /// Defines methods for using the custom rss feed infrastructure.
    /// </summary>
    public static class RssFeed
    {
        /// <summary>
        /// Adds a feed for the given module as a link element to the given page.
        /// </summary>
        /// <param name="page">The page to add the link to.</param>
        /// <param name="module">The module to add the link for.</param>
        public static void AddFeedToPage(this ModuleInfo module, Page page)
        {
            if (null == module)
                throw new ArgumentNullException("module");
            if (null == page)
                throw new ArgumentNullException("page");

            HtmlLink link = new HtmlLink
                                {
                                    Href = GenerateFeedUrl(module)
                                };
            link.Attributes["rel"] = "alternate";
            link.Attributes["type"] = "application/atom+xml";
            link.Attributes["title"] = module.ModuleTitle;
            page.Header.Controls.Add(link);
        }

        /// <summary>
        /// Generates the url to the custom feed for the given module.
        /// </summary>
        /// <param name="module">The module issuing the feed.</param>
        /// <returns>The url to the custom feed for the given module.</returns>
        public static string GenerateFeedUrl(this ModuleInfo module)
        {
            return String.Format(CultureInfo.InvariantCulture,
                                 "{0}{1}?tabid={2}&moduleid={3}{4}",
                                 HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority),
                                 VirtualPathUtility.ToAbsolute("~/DesktopModules/HeroldIT.ModuleFramework/RssFeed.aspx"),
                                 module.TabID, module.ModuleID,
                                 (Localization.GetEnabledLocales().Count > 1)
                                 && Localization.UseLanguageInUrl()
                                     ? "&language=" + CultureInfo.CurrentUICulture.Name
                                     : "");
        }

        /// <summary>
        /// Adds the host url to the given url if neccessary.
        /// </summary>
        /// <param name="url">The url to complete.</param>
        /// <returns>The completed url.</returns>
        public static string CompleteUrl(string url)
        {
            string domain = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
            if (url.StartsWith("~"))
            {
                return domain + VirtualPathUtility.ToAbsolute(url);
            }
            if (url.StartsWith(domain))
            {
                return url;
            }
            return domain + url;
        }
    }
}
